import {getPointGainWay, getPointRule} from "@src/api/points";
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "@src/store/base";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['businessName'],
  }],
  data: {},
  lifetimes: {
    attached: function () {
    },
    detached: function () {
    }
  },
  pageLifetimes: {},
  methods: {
    onLogin() {
      this.pointsGainWayReq();
      this.pointsRuleReq();
    },
    pointsRuleReq() {
      getPointRule().then(res => {
        if (res.code === 0) {
          let repData = res.data;
          let expirePointTitle = "";
          if (repData.expire) {
            expirePointTitle = `每年${repData.expireDate}定期清零上1年的积分`;
          } else {
            expirePointTitle = "积分永久有效";
          }
          repData.expirePointTitle = expirePointTitle;
          this.setData({pointsRuleData: repData})
          this.dillOpenCardAwardPoint(repData.categoryAwardPoints);
          this.dillRechargeSendPoint(repData.categoryAwardPoints);
        }
      })
    },
    dillOpenCardAwardPoint(categoryAwardPoints) {
      let openCardAwardPointArr = [];
      categoryAwardPoints.forEach(value => {
        if (value.category.giftPoints) {
          openCardAwardPointArr.push({
            cardName: value.category.categoryName,
            giftPoints: value.category.giftPoints
          });
        }
      });
      console.log("openCardAwardPointArr");
      console.log(openCardAwardPointArr);
      this.openCardAwardPointArr = openCardAwardPointArr;
      this.setData({"openCardAwardPointArr": openCardAwardPointArr})
    },
    dillRechargeSendPoint(categoryAwardPoints) {
      let rechargeSendPointArr = [];
      categoryAwardPoints.forEach(value => {
        value.rechargeAward.forEach(valueS => {
          if (valueS.giftPoints) {
            rechargeSendPointArr.push({
              cardName: value.category.categoryName,
              rechargeMoney: valueS.baseAmount / 100,
              giftPoints: valueS.giftPoints
            });
          }
        });
      });
      console.log("rechargeSendPointArr");
      console.log(rechargeSendPointArr);
      this.setData({"rechargeSendPointArr": rechargeSendPointArr})
    },

    pointsGainWayReq() {
      getPointGainWay().then(res => {
        if (res.code === 0) {
          this.setData({pointsGainWayData: res.data})
        }
      })
    }
  }
})
